import Axios from 'axios'
var baseURL = "http://192.168.1.68:18082/";
class httpRequest {
    constructor() {
            this.options = {
                    method: '',
                    url: '',
                }
                // 存储请求队列
            this.queue = {}
        }
        // 销毁请求实例
    destroy(url) {
            delete this.queue[url]
            const queue = Object.keys(this.queue)
            return queue.length
        }
        // 请求拦截
    interceptors(instance, url) {
            // 添加请求拦截器
            instance.interceptors.request.use(config => {
                if (window.localStorage.getItem('token')) {
                    config.headers.token = window.localStorage.getItem('token');
                 }
                // Spin.show()
                // 在发送请求之前做些什么
                return config
            }, error => {
                // 对请求错误做些什么
                return Promise.reject(error)
            })

            // 添加响应拦截器
            instance.interceptors.response.use((res) => {
                let {
                    data
                } = res
                const is = this.destroy(url)
                if (!is) {
                    setTimeout(() => {
                        // Spin.hide()
                    }, 500)
                }
                if (data.code !== 200) {
                    // 后端服务在个别情况下回报201，待确认
                    if (data.code === 401) {
                        Cookies.remove(TOKEN_KEY)
                        window.location.href = window.location.pathname + '#/login'
                        Message.error('未登录，或登录失效，请登录')
                    } else {
                        if (data.msg) Message.error(data.msg)
                    }
                    // return false
                    return data
                }
                return data
            }, (error) => {
                Message.error('服务内部错误')
                    // 对响应错误做点什么
                return Promise.reject(error)
            })
        }
        // 创建实例
    create() {
            let conf = {
                baseURL: baseURL,
                // mode: 'no-cors',
                // timeout: 2000,
                headers: {
                    'Content-Type': 'application/json; charset=utf-8;',
                    // 'X-Requested-With': 'XMLHttpRequest',
                    // 'X-URL-PATH': location.pathname,
                }
            }
            return Axios.create(conf)
        }
        // 合并请求实例
    mergeReqest(instances = []) {
        //
    }

    // 请求实例
    request(options) {
        var instance = this.create()
        instance.defaults.headers.post['Content-Type'] = 'application/json';
        // instance.defaults.headers.get['Content-Type'] = 'application/json';
        if (options.baseURL && options.baseURL != null) {
            instance.defaults.baseURL = options.baseURL
        }
        // this.interceptors(instance, options.url)
        options = Object.assign({}, options)
        this.queue[options.url] = instance
        this.interceptors(instance);
        return instance(options)
    }
}
export default httpRequest